package org.example.managere.controller;

import jakarta.validation.Valid;
import org.example.managere.dto.OperationDto;
import org.example.managere.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/operations")
public class OperationController {

    private final OperationService operationService;

    @Autowired
    public OperationController(OperationService operationService) {
        this.operationService = operationService;
    }

    @GetMapping
    public ResponseEntity<List<OperationDto>> getAllOperations() {
        return ResponseEntity.ok(operationService.getAllOperations());
    }

    @GetMapping("/page")
    public ResponseEntity<Page<OperationDto>> getOperationsWithPagination(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "name") String sortBy,
            @RequestParam(defaultValue = "asc") String direction) {
        
        Sort.Direction sortDirection = direction.equalsIgnoreCase("desc") ? 
                Sort.Direction.DESC : Sort.Direction.ASC;
        
        Pageable pageable = PageRequest.of(page, size, Sort.by(sortDirection, sortBy));
        return ResponseEntity.ok(operationService.getOperationsWithPagination(pageable));
    }

    @GetMapping("/bySystem")
    public ResponseEntity<List<OperationDto>> findBySystemContaining(@RequestParam String system) {
        return ResponseEntity.ok(operationService.findBySystemContaining(system));
    }
    
    @GetMapping("/bySubsystem")
    public ResponseEntity<List<OperationDto>> findBySubsystemContaining(@RequestParam String subsystem) {
        return ResponseEntity.ok(operationService.findBySubsystemContaining(subsystem));
    }
    
    @GetMapping("/byFMode")
    public ResponseEntity<List<OperationDto>> findByFModeContaining(@RequestParam String fMode) {
        return ResponseEntity.ok(operationService.findByFModeContaining(fMode));
    }

    @GetMapping("/{id}")
    public ResponseEntity<OperationDto> getOperationById(@PathVariable Integer id) {
        return ResponseEntity.ok(operationService.getOperationById(id));
    }

    @GetMapping("/name/{name}")
    public ResponseEntity<OperationDto> getOperationByName(@PathVariable String name) {
        return ResponseEntity.ok(operationService.getOperationByName(name));
    }

    @PostMapping
    public ResponseEntity<OperationDto> createOperation(@Valid @RequestBody OperationDto operationDto) {
        return new ResponseEntity<>(operationService.createOperation(operationDto), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<OperationDto> updateOperation(
            @PathVariable Integer id, 
            @Valid @RequestBody OperationDto operationDto) {
        return ResponseEntity.ok(operationService.updateOperation(id, operationDto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOperation(@PathVariable Integer id) {
        operationService.deleteOperation(id);
        return ResponseEntity.noContent().build();
    }
} 